using System;
using System.Collections.Generic;
using System.Linq;
using Backend.JYD.Model;
using Backend.JYD.Model.Cust;

namespace Backend.JYD.EFDAL.DAL
{
    public class CustPhoneDAL : Backend.JYD.IDAL.Cust.ICustPhone
    {
        public List<CustPhone> GetCustPhoneByCustNo(string custNo)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.CustPhones.Where(p => p.CustNo == custNo);
                return query == null ? null : query.ToList();
            }
        }
        public List<CustPhone> GetCustPhoneByPhone(string phone)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.CustPhones.Where(p => p.Phone == phone);
                return query == null ? null : query.ToList();
            }
        }

        public bool CheckCustPhone(string custNo, string phone)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.CustPhones.Where(p => p.CustNo == custNo && p.Phone == phone);
                return query.Count() > 0;
            }
        }

        public bool AddCustPhone(CustPhone info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                if (info.IsDefault)
                {
                    var query = db.CustPhones.Where(p => p.CustNo == info.CustNo && p.IsDefault == info.IsDefault).FirstOrDefault();
                    if (query != null)
                    {
                        query.UpdateTime = DateTime.Now;
                        query.IsDefault = false;
                        db.Entry(query).State = System.Data.Entity.EntityState.Modified;
                    }
                }

                info.CreateTime = DateTime.Now;
                info.Type = 1;
                info.Status = 1;
                info.UpdateTime = DateTime.Now;
                db.CustPhones.Add(info);
                return db.SaveChanges() > 0;
            }
        }
    }
}
